using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using Habanero.Faces.Base;
using Habanero.Faces.Win;

namespace Firestarter.DB.Schema.UI
{
    public class ImageStackControl: UserControlWin
    {
        private GridLayoutManager _gridLayoutManager;
        private IControlFactory _controlFactory;

        private Dictionary<object, ObjectPictureDetails> _objectPictureDetailses;
        private object _currentObject;

        private class ObjectPictureDetails
        {
            public IPictureBox PictureBox;
            public Image Unselected;
            public Image Selected;
        }

        public ImageStackControl()
        {
            _objectPictureDetailses = new Dictionary<object, ObjectPictureDetails>();
            _controlFactory = GlobalUIRegistry.ControlFactory;
            //ColumnLayoutManager columnLayoutManager= new ColumnLayoutManager(this, _controlFactory);
            //columnLayoutManager.ColumnCount = 1;
            _gridLayoutManager = new GridLayoutManager(this, _controlFactory);
            _gridLayoutManager.SetGridSize(5, 1);
            _gridLayoutManager.GapSize = 0;
            _gridLayoutManager.BorderSize = 0;
            _gridLayoutManager.FixAllRowsBasedOnContents();
        }

        public object CurrentObject
        {
            get { return _currentObject; }
        }

        private IPictureBox CreateAndAddPictureBox(Image image)
        {
            IPictureBox pictureBox = _controlFactory.CreatePictureBox();
            pictureBox.SizeMode = PictureBoxSizeMode.AutoSize;
            pictureBox.Image = image;
            _gridLayoutManager.AddControl(pictureBox);
            return pictureBox;
        }

        public void AddObjectImages(object obj, Image unselectedImage, Image selectedImage)
        {
            ObjectPictureDetails pictureDetails = new ObjectPictureDetails();
            pictureDetails.Unselected = unselectedImage;
            pictureDetails.Selected = selectedImage;
            pictureDetails.PictureBox = CreateAndAddPictureBox(unselectedImage);
            _objectPictureDetailses.Add(obj, pictureDetails);
        }

        public void SetCurrentObject(object obj)
        {
            ObjectPictureDetails pictureDetails;
            if (_currentObject != null)
            {
                pictureDetails = _objectPictureDetailses[_currentObject];
                pictureDetails.PictureBox.Image = pictureDetails.Unselected;
            }
            _currentObject = obj;
            pictureDetails = _objectPictureDetailses[obj];
            pictureDetails.PictureBox.Image = pictureDetails.Selected;
        }
    }
}
